﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

namespace WebApp.Controls
{
    public partial class BrandProduct_Interest : System.Web.UI.UserControl
    {
        private ProductManager m_ProductManager = new ProductManager();

        private int m_ShowCount = 3;
        public int ShowCount
        {
            set { m_ShowCount = value; }
        }

        private Cosmetic.DataEntity.Product m_Product;
        public Cosmetic.DataEntity.Product Product
        {
            set { m_Product = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProduct();
            }
        }

        private void BindProduct()
        {
            List<Cosmetic.DataEntity.ProductInfo> productList = new List<Cosmetic.DataEntity.ProductInfo>();
            productList = this.m_ProductManager.GetProductInfoByFirstCategorys(1);
             int theLenght = productList.Count;
             Random ran = new Random();
             int[] theNum = new int[m_ShowCount];
             int i = 0;
             //while (true)
             //{
             //    int tem = ran.Next(theLenght);
             //    if (!theNum.Contains(tem) && i < m_ShowCount)
             //    {
             //        theNum[i] = tem;
             //        i++;
             //    }
             //    else if (i == m_ShowCount)
             //    {
             //        break;
             //    }
             //}
             List<ProductInfo> showList = new List<ProductInfo>();
             if (theLenght < m_ShowCount)
             {
                 for (int j = 0; j < theLenght; j++)
                 {
                     showList.Add(productList[theNum[j]]);
                 }
             }
             else
             {

                 int tem = ran.Next(theLenght);
                 for (int j = tem; j < theLenght; j++)
                 {
                     if (i < m_ShowCount)
                     {
                         theNum[i] = j;
                         i++;
                     }
                     else
                     {
                         break;
                     }
                 }
                 //说明还没有将数组填充完。
                 int lastCount = m_ShowCount - i - 1;
                 i++;
                 if (i < m_ShowCount)
                 {
                     for (int a = 0; a < lastCount; a++)
                     {
                         theNum[i] = a;
                         i++;
                     }
                 }

                 for (int j = 0; j < m_ShowCount; j++)
                 {
                     showList.Add(productList[theNum[j]]);
                 }
             }
             this.lvProduct.DataSource = showList; //this.m_ProductManager.GetProductInfoByBrand(m_Product).OrderByDescending(theProduct => theProduct.Views).Take(m_ShowCount).ToList();
            this.lvProduct.DataBind();
        }

        protected void lvProduct_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Label lb = e.Item.FindControl("lbName") as Label;
            if (lb.Text.Length > 25)
            {
                lb.Text = lb.Text.Substring(0, 25)+"...";
            }
        }
    }
}